// pages/main/me/contract_mag/contract_index.js
import {baseUrl} from '../../../../utils/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pendingContracts: [],	
    signedContracts: [],					
    signTag: '',
    reviewUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // console.log('h5服务：', this.serviceUrl)
    // 处理上上签域名
    let url = '';
    if( baseUrl === 'https://recruit-dev.lgong.com/' ){
      url = `https://wx101.bestsign.info`
    }else if( baseUrl === 'https://recruit-test.lgong.com/' ){
      url = `https://wx101.bestsign.info`
    }else if( baseUrl === 'https://recruit.lgong.com/' ){
      url = `https://wx100.bestsign.cn:443`
    }
    this.setData({
      reviewUrl: url,
    })

  },

  // 获取合同列表
  getList: function(){
    let that = this;
    getApp().$http.getContractList().then(res => {
      console.log(res)
      let pendingContractsA = res.pendingContracts;
      let signedContractsA = res.signedContracts;
      that.setData({
        pendingContracts: pendingContractsA,
        signedContracts: signedContractsA,
      })
    })
  },

  // 签署合同按钮
  signBtn: function(e){
    let dataset = e.currentTarget.dataset
    let item = dataset["item"]
    wx.showLoading({
      title:'跳转中，请稍候'
    })
    let that = this;
    getApp().$http.singContract(item,function completed(params) {
      console.log(params)
      wx.hideLoading()					
      if(params !== 20050){
        wx.setStorageSync('CONTRACTNO', item.contractNo);
        let signingLink = params.signingLink
        signingLink = signingLink.replace(/^https:\/\/[^/]+/, that.data.reviewUrl)
        wx.setStorageSync('URL', signingLink);
        getApp().$routes.goTo(getApp().$routes.contract_sign)//去签署
      }else{
        wx.hideToast()
        wx.showModal({
          title: '温馨提示',
          content: '请您先完成实名认证',
          success: function (res) {
            if (res.confirm) {
              getApp().$routes.goTo(getApp().$routes.authentication)// 去实名认证
            } else if (res.cancel) {
              getApp().$routes.switchTab(getApp().$routes.mePage)// 去我的首页
            }
          }
        });
      }
    })
  },

  // 查看合同
  toDetail: function(e){
    let that = this;
    let dataset = e.currentTarget.dataset
    let id = dataset["name"]
    let obj = {
      "contractNo": id
    }
    wx.showLoading({
      title:'加载中，请稍等~'
    })
    getApp().$http.toContractDetail(obj).then(res => {
      console.log(res)
      let newUrl = res.replace(/^https:\/\/[^/]+/, that.data.reviewUrl)
      wx.setStorageSync('URLREVIEW', res);
      getApp().$routes.goTo(getApp().$routes.contract_look)//去签署
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.data.signTag = 0
    this.getList();



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})